namespace ghost
{
    export class BarrierSettingUI extends UIBase
    {
        public ctrlSp:Laya.Sprite;		
        /**陆路*/public check0:Laya.CheckBox;
		/**水路*/public check1:Laya.CheckBox;
		/**空路*/public check2:Laya.CheckBox;
		/**阴影*/public check3:Laya.CheckBox;
		
		
		/**提示文本*/public tip:Laya.Text;
        public _sceneMgr:SceneManager;		
	
		public mapInput:InputDialog;

        public ignoreBtn:Laya.Button;		
		public loadBtn:Laya.Button;
		public saveBtn:Laya.Button;
		public mapBtn:Laya.Button;
		
		// public tipImg:Laya.Image;
        public owner:BarrierUI;
		private _last_enabledBarrierSet:boolean;
		private _last_enabledTile:boolean;
		private _initX:number;
		private _initY:number;		
		private _tweenRunning:boolean;		
        constructor(o:BarrierUI)
        {
            super();
            let s= this;						
			s.height = 800;			
			s._initX = s.x = 10;
			s._initY = s.y = GameManager.DESIGN_HEIGHT - s.height + 4;
			s.owner = o;
            s._sceneMgr = s.owner._sceneMgr;
			s.title = "地图配置编辑";			
			BarrierSettingUILayout.getIntance().layout(s);
			s.ignoreBtn.on(Laya.Event.CLICK,s,s.oper);			
			s.saveBtn.on(Laya.Event.CLICK,s,s.oper);			
			s.loadBtn.on(Laya.Event.CLICK,s,s.oper);
			s.mapBtn.on(Laya.Event.CLICK,s,s.oper);			
			
						
			// s.tipImg.visible = false;			
			
			
			
			s.mapInput = new InputDialog;
			s.mapInput.title = "切换地图";           
			// s.on(Laya.Event.MOUSE_OVER, s, ()=>{
			// 	s.showTween();
			// });
			// s.on(Laya.Event.MOUSE_OUT, s, ()=>{
			// 	let mX:number,mY:number;
			// 	mX = s.mouseX;
			// 	mY = s.mouseY;
			// 	if(mX < s.x || mX > s.width || mY < s.y || mY > s.height)
			// 		s.hideTween();
			// });
			
			s.check0.selected = true;
        }		
		private showTween():void
		{
			let s= this;
			if(s._tweenRunning)return;
			s._tweenRunning = true;
			GYTween.to(s,[TweenData.getInstance("y",s._initY,NaN,GYTween.reduceEase)],100,0,s,()=>{
				s._tweenRunning = false;
			});
		}
		private hideTween():void
		{
			let s= this;
			if(s._tweenRunning)return;
			s._tweenRunning = true;
			GYTween.to(s,[TweenData.getInstance("y",s._initY + s.height*0.92,NaN,GYTween.reduceEase)],100,0,s,()=>{
				s._tweenRunning = false;
			});
		}
		
        
		protected oper(e:Laya.Event):void
		{			
			let s = this;
			
			super.oper(e);
			if(e.currentTarget == s.ignoreBtn)
			{
				s._sceneMgr.ignoreBarrier = s.ignoreBtn.selected;
			}
			
			else if(e.currentTarget == s.saveBtn)
			{
				// let cfg:ISceneConfig = s._sceneMgr.sceneCfg;			
				// let cfgStr:string;				
				// let dict:any = {};
				// let arr:any[] = s._sceneMgr.sceneCfg.roles;
				// let i:number, len:number;
				// let key:string;
				// if(arr && arr.length > 0)
				// {
				// 	len = arr.length;
				// 	for(i=0;i<len;++i)
				// 	{
				// 		key = (arr[i].absX|0) + "_" + (arr[i].absY|0);
				// 		if(dict[key] != null)
				// 		{
				// 			console.log("相同位置的元素!" + key, dict[key], arr[i]);
				// 			continue;
				// 		}
				// 		dict[key] = arr[i];
				// 	}
				// }
				
				// let editorData:IEditorData;
				// let barriers: any[];
				// //备份一下编辑器的数据
				// editorData = cfg.editorData;
				// barriers = cfg.barriers;
				// //保存前格式化一下配置				
				// cfg.barriers = logic.outputSaveBarrier(cfg);				
				// //输出areaPoints字段						
				// s.owner.outputAreaPoints();
				// //保存地图配置，地图配置仅包括地图拥有的信息，删除编辑器信息
				// if(cfg.editorData)
				// 	delete cfg.editorData;		
				// cfgStr = JSON.stringify(cfg);
				// CommonUtil.download(cfg.sceneInfo.mapName,cfgStr,{type:"application/json"})

				//保存工程，工程文件带有额外编辑信息
				// cfg = JSON.parse(cfgStr);
				// cfg.editorData = editorData;				
				// cfg.barriers = barriers;
				let str = JSON.stringify(SceneManager.instance.sceneCfg.barriers);
				str = str.replace(/null/g, "");
				CommonUtil.download(SceneManager.instance.mapName + "_proj",str,{type:"application/json"})
			}
			else if(e.currentTarget == s.mapBtn)
			{
				let s= this;
				s.mapInput.show(<Laya.Sprite>s.parent,null,(mapIdStr:string)=>{
					// let s= this;
					// let mapId:number = Number(mapIdStr);
					// if(mapId >= 9000)
					// 	s._sceneMgr.loadMap(mapId)
				});
			}			
			else if(e.currentTarget == s.loadBtn)
			{
				// CommonUtil.upload(function(blob:Blob):void{
				// 	let reader:FileReader = new FileReader;
				// 	reader.readAsText(blob,"utf-8");
				// 	reader.addEventListener("loadend", (e)=>{
				// 		let str:string = (<any>e.target).result;
				// 		let cfg:any = JSON.parse(str);						
				// 		SceneConfig.setCommonSceneConfig(cfg.sceneInfo.mapName,cfg);
				// 		(<MapEditBase>s._sceneMgr.sceneLogic).clearScene();
				// 		s._sceneMgr.resetSceneConfig(cfg);
				// 		s._sceneMgr.createSceneRole();
				// 		s.owner.reDrawBarrier();
				// 		s._sceneMgr.enableCloudBarrier(!s.owner.publicSetUI.ignoreMistBarrierCheck.selected);
				// 	});
				// },s,"application/json");
			}
		}
		public copyOper(e:Laya.Event):void
		{
			let s= this;
            if(window.navigator["clipboard"])
			    window.navigator["clipboard"].writeText(s._sceneMgr.sceneCfg.barriers.toString());
		}
        
		
		
		/**获取障碍值*/
        public getValue():number
		{
			let a0:number,a1:number,a2:number,a3:number;
			let s = this;
			a0 = s.check0.selected?1:0;
			a1 = s.check1.selected?1:0;
			a2 = s.check2.selected?1:0;
			a3 = s.check3.selected?1:0;
			return (a0 << 0) | (a1 << 1) | (a2 << 2) | (a3 << 3);
		}
		public show(pr: Laya.Sprite = null, data: any = null, commit: (data: any) => void = null):void
		{
			super.show(pr, data, commit);
			let s = this;
			Laya.timer.loop(500, s, s.tickLoop);
			// s.hideTween();
		}
		public hide():void
		{
			super.hide();
			let s = this;
			Laya.timer.clear(s, s.tickLoop);
		}
		private tickLoop():void
		{
			let s = this;
			let board:GYKeyboard;
			board = GYKeyboard.getInstance();
			s.tip.text = "ctrl:" + board.isCtrlDown() + ", alt:" + board.isAltDown() + ", shift:" + board.isShiftDown();
		}
    }
	export interface IEffectSettingCfg{
		/**id*/id?:number;		
		/**特效偏移X*/offsetX?:number;
		/**特效偏移Y*/offsetY?:number;
	}
}